package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.BusinessDTO;
import com.ktjy.dto.BusinessTrackRecordDTO;
import com.ktjy.pojo.TbAssignRecord;
import com.ktjy.pojo.TbBusiness;
import com.ktjy.pojo.TbContract;
import com.ktjy.service.ITbBusinessService;
import com.ktjy.service.ITbContractService;
import com.ktjy.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商机 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-08-19
 */
@RestController
@RequestMapping("/api/tb/business")
@Slf4j
@Tag(name="商机管理")
public class TbBusinessController {
    @Autowired
    ITbBusinessService businessService;
    @Autowired
    ITbContractService contractService;

    @Operation(summary="查询商机列表")
    @GetMapping("/list")
    public AjaxResult list(BusinessDTO businessDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        businessDTO.setStatuses(new String[]{"1","2"});
        IPage<TbBusiness> page = businessService.findPage(businessDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @Operation(summary="查询公海池")
    @GetMapping("/pool")
    public AjaxResult pool(BusinessDTO businessDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        businessDTO.setStatuses(new String[]{"3","4"});
        IPage<TbBusiness> page = businessService.findPage(businessDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @Operation(summary="新增商机")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody TbBusiness tbBusiness){
        try {
            businessService.saveBusiness(tbBusiness);
            return AjaxResult.success("保存成功");
        } catch (Exception e) {
            log.error("TbBusinessController中save方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @Operation(summary="修改商机")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody TbBusiness tbBusiness){
        try {
            boolean ok=businessService.updateById(tbBusiness);
            if(ok){
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("TbBusinessController中update方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @Operation(summary="查询商机详情")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id){
        //查询用户详情
        try {
            TbBusiness tbBusiness=businessService.getById(id);
            if(tbBusiness!=null){
                return AjaxResult.success(tbBusiness);
            }
        } catch (Exception e) {
            log.error("TbBusinessController中view方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @Operation(summary="删除商机")
    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            boolean ok= businessService.removeById(id);
            if(ok){//删除成功
                return AjaxResult.success("删除成功");
            }
        } catch (Exception e) {
            log.error("TbBusinessController中del方法执行报错的。",e);
        }
        return AjaxResult.error("删除失败");
    }

    @Operation(summary="分配商机")
    @PutMapping("/assignBusiness")
    public AjaxResult assignBusiness(@RequestBody TbAssignRecord assignRecord) {
        try {
            businessService.assignBusiness(assignRecord.getAssignId(), assignRecord.getUserId());
            return AjaxResult.success("分配成功");
        } catch (Exception e) {
            log.error("TbBusinessController中assign方法执行报错的。",e);
        }
        return AjaxResult.error("分配失败");
    }

    @Operation(summary="捞取商机")
    @PutMapping("/gainBusiness/{id}")
    public AjaxResult gainBusiness(@PathVariable("id") Long id) {
        try {
            businessService.gainBusiness(id);
            return AjaxResult.success("捞取商机成功");
        } catch (Exception e) {
            log.error("TbClueController中gainClue方法执行报错的。",e);
        }
        return AjaxResult.error("捞取商机失败");
    }

    @Operation(summary="退回公海")
    @PutMapping("/backBusiness")
    public AjaxResult backBusiness(@RequestBody BusinessTrackRecordDTO businessTrackRecordDTO) {
        try {
            businessService.backBusiness(businessTrackRecordDTO.getBusinessId(),
                    businessTrackRecordDTO.getBackReason());
            return AjaxResult.success("退回公海成功");
        } catch (Exception e) {
            log.error("TbBusinessController中backBusiness方法执行报错的。",e);
        }
        return AjaxResult.error("退回公海失败");
    }

    @Operation(summary="商机转合同")
    @PutMapping("/changeContract")
    public AjaxResult changeContract(@RequestBody TbContract tbContract) {
        try {
            contractService.changeContract(tbContract);
            return AjaxResult.success("商机转合同成功");
        } catch (Exception e) {
            log.error("TbBusinessController中changeContract方法执行报错的。",e);
        }
        return AjaxResult.error("商机转合同失败");
    }
}
